package com.yj.suncity.activity;

import java.io.IOException;

import org.json.JSONObject;

import com.yj.suncity.R;
import com.yj.suncity.async.ThreadUtils;
import com.yj.suncity.async.ThreadUtils.OnResultListener;
import com.yj.suncity.constant.ShareCon;
import com.yj.suncity.constant.URLCon;
import com.yj.suncity.publish.PublishAdvertiseActivity;
import com.yj.suncity.publish.PublishCityHelpActivity;
import com.yj.suncity.publish.PublishHuodongActivity;
import com.yj.suncity.publish.PublishProductActivity;
import com.yj.suncity.publish.PublishXuanshangActivity;
import com.yj.suncity.user.LoginActivity;
import com.yj.suncity.user.UpdateShopInfoActivity;
import com.yj.suncity.user.bean.EntBean;
import com.yj.suncity.utis.HttpUtil;
import com.yj.suncity.utis.SharedPreferencesUtil;
import com.yj.suncity.utis.ToastUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.RelativeLayout;

public class PublishActivity extends Activity {
	private Context context;
	private SharedPreferencesUtil preferencesUtil;
	private String userId;
	private boolean isShop;
	private boolean isLogin;
	private boolean isUserInfo;
	private String entId;// 企业ID
	private RelativeLayout mLay_rela_all_info;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_publish);
		context = this;
		preferencesUtil = SharedPreferencesUtil.getInstance();
		mLay_rela_all_info = (RelativeLayout) findViewById(R.id.lay_rela_all_info);
		mLay_rela_all_info.setVisibility(View.GONE);
	}

	@Override
	protected void onResume() {
		isLogin = preferencesUtil.getBoolean(context, ShareCon.USERFILE,
				ShareCon.ISLOGIN, false);
		if (isLogin) {
			userId = preferencesUtil.getString(context, ShareCon.USERFILE,
					ShareCon.USERID, "");
			entId = preferencesUtil.getString(context, ShareCon.USERFILE,
					ShareCon.SHOPID, "");
			isShop = preferencesUtil.getBoolean(context, ShareCon.USERFILE,
					ShareCon.USERTYPE, false);
			if (isShop) {
				getShopInfo();
			}
		} else {
			mLay_rela_all_info.setVisibility(View.GONE);
			userId = "";
			entId="";
			isShop = false;
		}
		super.onResume();
	}

	private void getShopInfo() {
		final String path = URLCon.用户 + "?action=getentinfobyentid&entid="
				+ entId;
		ThreadUtils threadUtils = new ThreadUtils();
		threadUtils.startThread(path, new OnResultListener() {
			@Override
			public void jsonResult(JSONObject json) {
				EntBean entBean = new EntBean(json);
				if (entBean.getEnt_contact().equals("")
						|| entBean.getEnt_logo().equals("")
						|| entBean.getEnt_contact_tel().equals("")
						|| entBean.getEnt_address().equals("")) {
					isUserInfo = false;
					mLay_rela_all_info.setVisibility(View.VISIBLE);
				} else {
					isUserInfo = true;
					mLay_rela_all_info.setVisibility(View.GONE);
				}

			}
		});
	}

	private boolean getShopInfoType() {
		if (!isUserInfo) {
			ToastUtil.ShowToast(context, "商户资料还不完整，请补充资料后发布");
			return false;
		}
		return true;
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_all_info:// 补充完整信息
			Intent it = new Intent(context, UpdateShopInfoActivity.class);
			it.putExtra("titlename", "补充资料");
			it.putExtra("userid", userId);
			it.putExtra("entid", entId);
			startActivity(it);
			break;

		case R.id.lay_rela_1_1:
			if (isLogin) {
				if (isShop) {// 发布广告
					if (!getShopInfoType())
						return;

					Intent intent = new Intent(context,
							PublishAdvertiseActivity.class);
					intent.putExtra("userid", userId);
					startActivity(intent);
				} else {
					ToastUtil.ShowToast(context, "您还不是商户！");
				}
			} else {
				startLoginActivity();
			}
			break;
		case R.id.lay_rela_1_2:
			if (isLogin) {// 发布悬赏
				if (isShop) {
					if (!getShopInfoType())
						return;
					Intent intent = new Intent(context,
							PublishXuanshangActivity.class);
					intent.putExtra("userid", userId);
					startActivity(intent);
				} else {
					ToastUtil.ShowToast(context, "您还不是商户！");
				}
			} else {
				startLoginActivity();
			}
			break;
		case R.id.lay_rela_1_3:
			if (isLogin) {
				if (isShop) {

				} else {
					ToastUtil.ShowToast(context, "您还不是商户！");
				}
			} else {
				startLoginActivity();
			}
			break;
		case R.id.lay_rela_2_1:// 发布同城帮帮
			if (isLogin) {
				Intent intent = new Intent(context,
						PublishCityHelpActivity.class);
				intent.putExtra("userid", userId);
				startActivity(intent);
			} else {
				startLoginActivity();
			}
			break;
		case R.id.lay_rela_2_2:
			if (isLogin) {
				if (isShop) {
					if (!getShopInfoType())
						return;
					Intent intent = new Intent(context,
							PublishProductActivity.class);
					intent.putExtra("userid", userId);
					intent.putExtra("release", "1");// 商家商品
					startActivity(intent);
				} else {
					ToastUtil.ShowToast(context, "您还不是商户！");
				}
			} else {
				startLoginActivity();
			}
			break;
		case R.id.lay_rela_2_3:
//			if (isLogin) {
//				if (isShop) {
//					if (!getShopInfoType())
//						return;
//					Intent intent = new Intent(context,
//							PublishProductActivity.class);
//					intent.putExtra("userid", userId);
//					intent.putExtra("release", "2");// 抵购商品
//					startActivity(intent);
//				} else {
//					ToastUtil.ShowToast(context, "您还不是商户！");
//				}
//			} else {
//				startLoginActivity();
//			}
			ToastUtil.ShowToast(context, "暂未开放......");
			break;
		case R.id.lay_rela_3_1:
			if (isLogin) {
				if (isShop) {// 发布活动
					if (!getShopInfoType())
						return;
					Intent intent = new Intent(context,
							PublishHuodongActivity.class);
					intent.putExtra("userid", userId);
					startActivity(intent);
				} else {
					ToastUtil.ShowToast(context, "您还不是商户！");
				}
			} else {
				startLoginActivity();
			}
			break;
		case R.id.lay_rela_3_2:
			if (isLogin) {
				if (isShop) {

				} else {
					ToastUtil.ShowToast(context, "您还不是商户！");
				}
			} else {
				startLoginActivity();
			}
			break;
		case R.id.lay_rela_3_3:
			if (isLogin) {
				if (isShop) {

				} else {
					ToastUtil.ShowToast(context, "您还不是商户！");
				}
			} else {
				startLoginActivity();
			}
			break;
		}
	}

	private void startLoginActivity() {
		startActivity(new Intent(context, LoginActivity.class));
	}
}
